package tournament.volleyball;

import java.util.ArrayList;

public class Rivalry {
	private static int nextId = 0;
	private int id;
	
	protected Team team1;
	protected Team team2;
	
	protected ArrayList<Game> games;
	
	private Rivalry() {
	}

	public Rivalry(Team team1, Team team2) {
		super();
		this.team1 = team1;
		this.team2 = team2;
		
		this.id = nextId;
		Rivalry.nextId++;
		
		games = new ArrayList<Game>();
	}

	public Rivalry(Team team1, Team team2, Game solo){
		super();
		this.team1 = team1;
		this.team2 = team2;
		
		this.id = nextId;
		Rivalry.nextId++;
		
		games = new ArrayList<Game>();
		games.add(solo);
	}

	protected void initialize(int gamesPerSet){
		for (int i=0; i<gamesPerSet; i++) {
			games.add(new Game(this));
		}
	}

	public Team getTeam1() {
		return team1;
	}

	public Team getTeam2() {
		return team2;
	}
	
	// Adds a game just like the above initialize function
	public void addGame() {
		games.add(new Game(this));
	}
	
	// Adds an existing game to a Rivalry. WILL CHANGE TEAMS!!!
	public void addGame(Game added) {
		added.setParent(this);
		games.add(added);
	}

	public boolean hasTeam(Team team) {
		if(team1.equals(team) || team2.equals(team)) {
			return true;
		} else {
			return false;
		}
	}

	public Team getOpponent(Team team) {
		if (team.equals(team1)) {
			return team2;
		} else if (team.equals(team2)) {
			return team1;
		} else {
			return null;
		}
	}

	public int getWins(Team winner) {
		int wins = 0;
		
		for (int i=0; i<games.size(); i++) {
			wins += games.get(i).getWin(winner);
		}
		return wins;
	}
	
	public int getLosses(Team loser) {
		int losses = 0;
		
		for (int i=0; i<games.size(); i++) {
			losses += games.get(i).getLoss(loser);
		}
		return losses;
	}
	
	public Game getGame(int i) {
		return games.get(i);
	}
	
	public int getId() {
		return id;
	}

}